/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viettel.bkno.visionone.untils;

import com.viettel.bkno.visionone.exception.CoreServiceException;
import com.viettel.bkno.visionone.exception.CoreServiceFault;
import com.viettel.bkno.visionone.exception.FaultType;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

/**
 *
 * @author hienpt9
 */
public class ConfigurationManager {

    Logger logger = Logger.getLogger(ConfigurationManager.class);
    static ConfigurationManager instance = new ConfigurationManager();

    // SYSTEM CONFIG FILE PATH
    //String SYSTEM_CONFIG_FILE_PATH = "/home/hienpt9/Desktop/Visionone/config/visionone.cfg";
    //String SYSTEM_CONFIG_FILE_PATH = "/home/hien/Desktop/bkno-webpi-read-only/2 Source Code/core service/config/visionone.cfg";
    String SYSTEM_CONFIG_FILE_PATH ="../config/visionone.cfg";
    Properties configProperties = new Properties();
    FileOutputStream fileOutput;
    FileInputStream fileInput;

    private ConfigurationManager() {
        BasicConfigurator.configure();
        logger.info("Log4j default configuration applied.");
        
    }

    public static ConfigurationManager getInstance() {
        return instance;
    }
    
    /**
     * Load toan bo thong tin config cua he thong
     */
    public void load()  {
        try {
            fileInput = new FileInputStream(SYSTEM_CONFIG_FILE_PATH);
            configProperties.load(fileInput);
        } catch (FileNotFoundException ex) {
            logger.error("Can find the configuration file.", ex);
        } catch (IOException ex) {
            logger.error("Can load configuration.", ex);
        }
    }

    /**
     * Ghi toan bo config tu Properties xuong file
     * @throws java.io.IOException
     */
    public void configSynchronize() throws IOException {
        configProperties.store(fileOutput, "synchroized " + (new Date()).toString());
    }

    /**
     * Doc thong so cau hinh
     *
     * @param parameterName
     * @return
     * @throws com.viettel.bkno.visionone.exception.CoreServiceException
     */
    public String getConfigValue(ConfigType parameterName) throws CoreServiceException {
        String value = configProperties.getProperty(parameterName.getParameterName());
        if (value == null || value.equals("")) {
            logger.warn(parameterName.getParameterName() + " is not set.");
            CoreServiceFault fault = new CoreServiceFault();
            fault.setFaultCode(FaultType.PARAMETER_UNSET_EXCEPTION.getCode());
            fault.setFaultCode(FaultType.PARAMETER_UNSET_EXCEPTION.getName());
            throw new CoreServiceException(fault);
        }
        return value;
    }
}
